package com.punai.dataanalysis.epdatakb.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.DateUtils;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisNdgclpdqkhzb;
import com.punai.dataanalysis.epdatakb.service.NG001change1212.NG001YearGcXmCbC1212Service;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.dataanalysis.ng001.bo.YearLjWccz;
import com.punai.dataanalysis.ng001.domain.ProjectTable;
import com.punai.dataanalysis.ng001.mapper.ProjectTableMapper;
import com.punai.dataanalysis.ng001.mapper.YearJssjcbMapper;
import com.punai.prodatalibrary.domain.bo.GlobalProInfoBo;
import com.punai.prodatalibrary.service.ILibProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.dataanalysis.epdatakb.mapper.DataAnalysisNdjsgcxmsjfxhzbMapper;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisNdjsgcxmsjfxhzb;
import com.punai.dataanalysis.epdatakb.service.IDataAnalysisNdjsgcxmsjfxhzbService;

/**
 * 年度建设工程项目实际成本数据分析汇总表 Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-20
 */
@Service
public class DataAnalysisNdjsgcxmsjfxhzbServiceImpl extends ServiceImpl<DataAnalysisNdjsgcxmsjfxhzbMapper,DataAnalysisNdjsgcxmsjfxhzb> implements IDataAnalysisNdjsgcxmsjfxhzbService
{
    @Autowired
    private DataAnalysisNdjsgcxmsjfxhzbMapper dataAnalysisNdjsgcxmsjfxhzbMapper;

    @Autowired
    private ProjectTableMapper projectTableMapper;

    @Autowired
    private ILibProjectInfoService libProjectInfoService;

    @Autowired
    private YearJssjcbMapper yearJssjcbMapper;

    @Autowired
    private NG001YearGcXmCbC1212Service ng001YearGcXmCbC1212Service;

    @Override
    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {

        if (isC1212) {
            List<? extends BaseEntity> dataList = ng001YearGcXmCbC1212Service.getDataList(dataAnalysisVo);
            return dataList;
        }
        List<GlobalProInfoBo> list = libProjectInfoService.selectInfoListNoPerm(new GlobalProInfoBo());
        List<DataAnalysisNdjsgcxmsjfxhzb> res = new ArrayList<>();
        List<String> pmInfoIds = list.stream().map(GlobalProInfoBo::getPmproinfoid).collect(Collectors.toList());
        List<ProjectTable> projectTables = projectTableMapper.selectNdjsgcxmsjfxhzbListByVo(pmInfoIds);
        pmInfoIds = projectTables.stream().map(ProjectTable::getPhid).filter(Objects::nonNull).collect(Collectors.toList());
        if (CollUtil.isEmpty(pmInfoIds)) {
            return new ArrayList<>();
        }
        Map<String, ProjectTable> projectTablesMap = projectTables.stream().collect(Collectors.toMap(ProjectTable::getPhid, Function.identity(), (v1, v2) -> v2));
        List<JtczResultBo> xxjd = projectTableMapper.xxjd(pmInfoIds);
        Map<String, String> xxjdMap = xxjd.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVals, (v1, v2) -> v2));

//        List<JtczResultBo> glrnsl = projectTableMapper.glrnsl(pmInfoIds);
//        Map<String, BigDecimal> glrnslMap = glrnsl.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));

        List<YearLjWccz> wnYs = wnYs(pmInfoIds);
        Map<String, YearLjWccz> wnYsMap = wnYs.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> wnCb = wnCb(pmInfoIds);
        Map<String, YearLjWccz> wnCbMap = wnCb.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> bnCz = bnCz(pmInfoIds);
        Map<String, YearLjWccz> bnCzMap = bnCz.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> bnCb = bnCb(pmInfoIds);
        Map<String, YearLjWccz> bnCbMap = bnCb.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> ljCb = ljCb(pmInfoIds);
        Map<String, YearLjWccz> ljCbMap = ljCb.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));


        for (GlobalProInfoBo globalProInfoBo : list) {
            DataAnalysisNdjsgcxmsjfxhzb sjcb = new DataAnalysisNdjsgcxmsjfxhzb();
            if(StrUtil.isNotEmpty(globalProInfoBo.getPmproinfoid())){
                String pc = globalProInfoBo.getPmproinfoid();
                ProjectTable projectTable = projectTablesMap.get(pc);
                if (projectTable == null) {
                    continue;
                }
                sjcb = DataAnalysisNdjsgcxmsjfxhzb.conversion(sjcb,projectTable);
                YearLjWccz wnys = wnYsMap.get(pc);
                YearLjWccz wncb = wnCbMap.get(pc);
                YearLjWccz bncz = bnCzMap.get(pc);
                YearLjWccz bncb = bnCbMap.get(pc);
                YearLjWccz ljcb = ljCbMap.get(pc);
                // excel 变更了   本年变成了  往年
                // 但是字段还是用本年的 所以看起来会有歧义
                sjcb.setXxjd(xxjdMap.getOrDefault(pc,""));
                sjcb.setYearqkYsCzHj(wnys.getPdzhj());
                sjcb.setYearqkCzSj(wnys.getSj());
                sjcb.setYearqkCzJgc(wnys.getJgc());

                sjcb.setKlqkCostHj(wncb.getPdzhj());
                sjcb.setKlqkCostJgc(wncb.getJgc());

                sjcb.setYearCzHj(bncz.getPdzhj());
                sjcb.setYearCzSj(bncz.getSj());
                sjcb.setYearCzJgc(bncz.getJgc());

                sjcb.setYearCostHj(bncb.getPdzhj());
                sjcb.setYearCostJgc(bncb.getJgc());

                sjcb.setKlCostHj(ljcb.getPdzhj());
                sjcb.setKlCostJgc(ljcb.getJgc());

                calcZb(sjcb);

            }
            res.add(sjcb);
        }
        DataAnalysisNdjsgcxmsjfxhzb zj = zj(res);
        zj.setProName("总计");
        res = xmbFz(res);
        res.add(zj);
        return res;
    }

    private List<DataAnalysisNdjsgcxmsjfxhzb> xmbFz(List<DataAnalysisNdjsgcxmsjfxhzb> res) {
        List<DataAnalysisNdjsgcxmsjfxhzb> newResList = new ArrayList<>();
        Map<String, List<DataAnalysisNdjsgcxmsjfxhzb>> xmbGroup = res.stream().filter(e -> StrUtil.isNotBlank(e.getDeptName())).collect(Collectors.groupingBy(DataAnalysisNdjsgcxmsjfxhzb::getDeptName));
        for (Map.Entry<String, List<DataAnalysisNdjsgcxmsjfxhzb>> entry : xmbGroup.entrySet()) {
            String key = entry.getKey();
            List<DataAnalysisNdjsgcxmsjfxhzb> value = entry.getValue();
            DataAnalysisNdjsgcxmsjfxhzb zj = zj(value);
            zj.setProName(key + "小计");
            newResList.addAll(value);
            newResList.add(zj);
        }
        return newResList;
    }

    private DataAnalysisNdjsgcxmsjfxhzb zj(List<DataAnalysisNdjsgcxmsjfxhzb> value) {
        DataAnalysisNdjsgcxmsjfxhzb re = new DataAnalysisNdjsgcxmsjfxhzb();
        BigDecimal htTotal = BigDecimal.ZERO;
        BigDecimal jzmj = BigDecimal.ZERO;
        BigDecimal yearqkYsCzHj = BigDecimal.ZERO;
        BigDecimal yearqkYsCzZyfb = BigDecimal.ZERO;
        BigDecimal yearqkCzSj = BigDecimal.ZERO;
        BigDecimal yearqkCzJgc = BigDecimal.ZERO;
        BigDecimal klqkCostHj = BigDecimal.ZERO;
        BigDecimal klqkCostZyfb = BigDecimal.ZERO;
        BigDecimal klqkCostJgc = BigDecimal.ZERO;
        BigDecimal klqkCostYearPd = BigDecimal.ZERO;
        BigDecimal yearCzHj = BigDecimal.ZERO;
        BigDecimal yearCzZyfb = BigDecimal.ZERO;
        BigDecimal yearCzSj = BigDecimal.ZERO;
        BigDecimal yearCzJgc = BigDecimal.ZERO;
        BigDecimal yearCostHj = BigDecimal.ZERO;
        BigDecimal yearCostZyfb = BigDecimal.ZERO;
        BigDecimal yearCostJgc = BigDecimal.ZERO;
        BigDecimal yearLr = BigDecimal.ZERO;
        BigDecimal klCzYsHj = BigDecimal.ZERO;
        BigDecimal klCzYsZyfb = BigDecimal.ZERO;
        BigDecimal klCzSj = BigDecimal.ZERO;
        BigDecimal klCzJgc = BigDecimal.ZERO;
        BigDecimal klCostHj = BigDecimal.ZERO;
        BigDecimal klCostZyfb = BigDecimal.ZERO;
        BigDecimal klCostJgc = BigDecimal.ZERO;
        BigDecimal klLr = BigDecimal.ZERO;
        BigDecimal htkhSjzhfy = BigDecimal.ZERO;
        BigDecimal htkhXmdfp = BigDecimal.ZERO;
        for (DataAnalysisNdjsgcxmsjfxhzb jscg : value) {
            htTotal = NumberUtil.add(htTotal, jscg.getHtTotal());
            jzmj = NumberUtil.add(jzmj, jscg.getJzmj());
            yearqkYsCzHj = NumberUtil.add(yearqkYsCzHj, jscg.getYearqkYsCzHj());
            yearqkYsCzZyfb = NumberUtil.add(yearqkYsCzZyfb, jscg.getYearqkYsCzZyfb());
            yearqkCzSj = NumberUtil.add(yearqkCzSj, jscg.getYearqkCzSj());
            yearqkCzJgc = NumberUtil.add(yearqkCzJgc, jscg.getYearqkCzJgc());
            klqkCostHj = NumberUtil.add(klqkCostHj, jscg.getKlqkCostHj());
            klqkCostZyfb = NumberUtil.add(klqkCostZyfb, jscg.getKlqkCostZyfb());
            klqkCostJgc = NumberUtil.add(klqkCostJgc, jscg.getKlqkCostJgc());
            klqkCostYearPd = NumberUtil.add(klqkCostYearPd, jscg.getKlqkCostYearPd());
            yearCzHj = NumberUtil.add(yearCzHj, jscg.getYearCzHj());
            yearCzZyfb = NumberUtil.add(yearCzZyfb, jscg.getYearCzZyfb());
            yearCzSj = NumberUtil.add(yearCzSj, jscg.getYearCzSj());
            yearCzJgc = NumberUtil.add(yearCzJgc, jscg.getYearCzJgc());
            yearCostHj = NumberUtil.add(yearCostHj, jscg.getYearCostHj());
            yearCostZyfb = NumberUtil.add(yearCostZyfb, jscg.getYearCostZyfb());
            yearCostJgc = NumberUtil.add(yearCostJgc, jscg.getYearCostJgc());
            yearLr = NumberUtil.add(yearLr, jscg.getYearLr());
            klCzYsHj = NumberUtil.add(klCzYsHj, jscg.getKlCzYsHj());
            klCzYsZyfb = NumberUtil.add(klCzYsZyfb, jscg.getKlCzYsZyfb());
            klCzSj = NumberUtil.add(klCzSj, jscg.getKlCzSj());
            klCzJgc = NumberUtil.add(klCzJgc, jscg.getKlCzJgc());
            klCostHj = NumberUtil.add(klCostHj, jscg.getKlCostHj());
            klCostZyfb = NumberUtil.add(klCostZyfb, jscg.getKlCostZyfb());
            klCostJgc = NumberUtil.add(klCostJgc, jscg.getKlCostJgc());
            klLr = NumberUtil.add(klLr, jscg.getKlLr());
            htkhSjzhfy = NumberUtil.add(htkhSjzhfy, jscg.getHtkhSjzhfy());
            htkhXmdfp = NumberUtil.add(htkhXmdfp, jscg.getHtkhXmdfp());
        }

        re.setHtTotal(htTotal);
        re.setJzmj(jzmj);
        re.setYearqkYsCzHj(yearqkYsCzHj);
        re.setYearqkYsCzZyfb(yearqkYsCzZyfb);
        re.setYearqkCzSj(yearqkCzSj);
        re.setYearqkCzJgc(yearqkCzJgc);
        re.setKlqkCostHj(klqkCostHj);
        re.setKlqkCostZyfb(klqkCostZyfb);
        re.setKlqkCostJgc(klqkCostJgc);
        re.setKlqkCostYearPd(klqkCostYearPd);
        re.setYearCzHj(yearCzHj);
        re.setYearCzZyfb(yearCzZyfb);
        re.setYearCzSj(yearCzSj);
        re.setYearCzJgc(yearCzJgc);
        re.setYearCostHj(yearCostHj);
        re.setYearCostZyfb(yearCostZyfb);
        re.setYearCostJgc(yearCostJgc);
        re.setYearLr(yearLr);
        re.setKlCzYsHj(klCzYsHj);
        re.setKlCzYsZyfb(klCzYsZyfb);
        re.setKlCzSj(klCzSj);
        re.setKlCzJgc(klCzJgc);
        re.setKlCostHj(klCostHj);
        re.setKlCostZyfb(klCostZyfb);
        re.setKlCostJgc(klCostJgc);
        re.setKlLr(klLr);
        re.setHtkhSjzhfy(htkhSjzhfy);
        re.setHtkhXmdfp(htkhXmdfp);
        re.hb();
        calcZb(re);
        return re;
    }

    private void calcZb(DataAnalysisNdjsgcxmsjfxhzb sjcb) {
        // 往年年终盘点利润情况分析
        BigDecimal yearqkYsCzHj = sjcb.getYearqkYsCzHj();
        BigDecimal yearqkCzJgc = sjcb.getYearqkCzJgc();
        BigDecimal klCostHj = sjcb.getKlqkCostHj();
        // 往年年终盘点利润情况（元） = 往年累计完成产值情况 - 往年累计发生成本情况
        BigDecimal wnpdlr = NumberUtil.subtract(yearqkYsCzHj, klCostHj);
        sjcb.setKlqkCostYearPd(wnpdlr);
        // 往年年终盘点利润率（%） = 往年年终盘点利润情况（元） / (往年累计完成产值情况 + 往年累计完成产值情况甲供材 )
        BigDecimal wnpdlrl = NumberUtil.divideUseProportion(wnpdlr, 2, NumberUtil.add(yearqkYsCzHj, yearqkCzJgc));
        sjcb.setKlqkCostYearPdLrl(wnpdlrl);


        // 本年利润情况分析
        BigDecimal czHj = sjcb.getYearCzHj();
        BigDecimal jgc = sjcb.getYearCzJgc();
        BigDecimal costHj = sjcb.getYearCostHj();
        // 与上面一致
        BigDecimal bnpdlr = NumberUtil.subtract(czHj, costHj);
        sjcb.setYearLr(bnpdlr);
        BigDecimal bnpdlrl = NumberUtil.divideUseProportion(bnpdlr, 2, NumberUtil.add(czHj, jgc));
        sjcb.setYearLrl(bnpdlrl);

        // 累计利润考核情况分析 与上面一致
        BigDecimal klCzYsHj = sjcb.getKlCzYsHj();
        BigDecimal klCzJgc = sjcb.getKlCzJgc();
        BigDecimal klCostHj1 = sjcb.getKlCostHj();
        BigDecimal kllr = NumberUtil.subtract(klCzYsHj, klCostHj1);
        sjcb.setKlLr(kllr);
        BigDecimal kllrl = NumberUtil.divideUseProportion(kllr, 2, NumberUtil.add(klCzYsHj, klCzJgc));
        sjcb.setKlLrl(kllrl);

    }

    private List<YearLjWccz> wnYs(List<String> pmInfoIds ) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> ysHj = yearJssjcbMapper.wnYsHj(pmInfoIds);
        Map<String, BigDecimal> ysHjMap = ysHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> ysSj = yearJssjcbMapper.wnYsSj(pmInfoIds);
        Map<String, BigDecimal> ysSjMap = ysSj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> ysJgc = yearJssjcbMapper.wnYsJgc(pmInfoIds);
        Map<String, BigDecimal> ysJgcMap = ysJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));

        for (String pc : pmInfoIds) {
            YearLjWccz re = new YearLjWccz();
            re.setPc(pc);
            re.setPdzhj(ysHjMap.getOrDefault(pc,BigDecimal.ZERO));
            re.setSj(ysSjMap.getOrDefault(pc,BigDecimal.ZERO));
            re.setJgc(ysJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(re);
        }
        return resList;
    }

    private List<YearLjWccz> wnCb(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> cbHj = yearJssjcbMapper.wnCbHj(pmInfoIds);
        Map<String, BigDecimal> cbHjMap = cbHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> cbJgc = yearJssjcbMapper.wnCbJgc(pmInfoIds);
        Map<String, BigDecimal> cbJgcMap = cbJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        for (String pc : pmInfoIds) {
            YearLjWccz re = new YearLjWccz();
            re.setPc(pc);
            re.setPdzhj(cbHjMap.getOrDefault(pc,BigDecimal.ZERO));
            re.setJgc(cbJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(re);
        }
        return resList;
    }

    private List<YearLjWccz> bnCz(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> czHj = yearJssjcbMapper.bnCzHj(pmInfoIds);
        Map<String, BigDecimal> czHjMap = czHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> czSj = yearJssjcbMapper.bnCzSj(pmInfoIds);
        Map<String, BigDecimal> czSjMap = czSj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> czJgc = yearJssjcbMapper.bnCzJgc(pmInfoIds);
        Map<String, BigDecimal> czJgcMap = czJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        for (String pc : pmInfoIds) {
            YearLjWccz re = new YearLjWccz();
            re.setPc(pc);
            re.setPdzhj(czHjMap.getOrDefault(pc,BigDecimal.ZERO));
            re.setSj(czSjMap.getOrDefault(pc,BigDecimal.ZERO));
            re.setJgc(czJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(re);
        }

        return resList;
    }

    private List<YearLjWccz> bnCb(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> cbHj = yearJssjcbMapper.bnCbHj(pmInfoIds);
        Map<String, BigDecimal> cbHjMap = cbHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> cbJgc = yearJssjcbMapper.bnCbJgc(pmInfoIds);
        Map<String, BigDecimal> cbJgcMap = cbJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        for (String pc : pmInfoIds) {
            YearLjWccz re = new YearLjWccz();
            re.setPc(pc);
            re.setPdzhj(cbHjMap.getOrDefault(pc,BigDecimal.ZERO));
            re.setJgc(cbJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(re);
        }
        return resList;
    }


    private List<YearLjWccz> ljCb(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> cbHj = yearJssjcbMapper.ljCbHj(pmInfoIds);
        Map<String, BigDecimal> cbHjMap = cbHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> cbJgc = yearJssjcbMapper.ljCbJgc(pmInfoIds);
        Map<String, BigDecimal> cbJgcMap = cbJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        for (String pc : pmInfoIds) {
            YearLjWccz re = new YearLjWccz();
            re.setPc(pc);
            re.setPdzhj(cbHjMap.getOrDefault(pc,BigDecimal.ZERO));
            re.setJgc(cbJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(re);
        }
        return resList;
    }

}
